package com.lyc.common.config;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author 李大神仙
 * @Date 2024/3/19 21:54
 * @Version 1.0
 */
@Configuration
@EnableSwagger2
public class Knife4jConfiguration {
    /**
     * 定义分隔符
     */
    private static final String SPLITOR = ";";

    @Bean(value = "createRestApil")
    public Docket createRestApil(){
        //指定使用swagger2规范
        /*Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                    //描述字段支持Markdown语法
                    .description("HR应用系统服务")
                    .title("HR系统服务")
                    .version("1.0")
                    .build())
                //分组名称
                .groupName("业务服务")
                .select()
                //指定controller扫描包路径
                .apis(RequestHandSelectors.basePackage("com.sunwoda.depart.business.interfaces.controller"))
                .paths(PathSelectors.any())
                .buld();
        return docket;*/
        return null;
    }

    @Bean(value = "createRestApi2")
    public Docket createRestApi2(){
        //指定使用swagger2规范
       /* Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //描述字段支持Markdown语法
                        .description("HR应用系统服务")
                        .title("HR系统服务")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("系统服务")
                .select()
                //指定controller扫描包路径
                .apis(RequestHandSelectors.basePackage("com.sunwoda.depart.system.interfaces.controller"))
                .paths(PathSelectors.any())
                .buld();
        return docket;*/
        return null;
    }

    @Bean(value = "createRestApi3")
    public Docket createRestApi3(){
        //指定使用swagger2规范
       /* Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //描述字段支持Markdown语法
                        .description("HR应用系统服务")
                        .title("HR系统服务")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("系统服务")
                .select()
                //指定controller扫描包路径
                .apis(RequestHandSelectors.basePackage("com.sunwoda.depart.common.interfaces.controller"))
                .paths(PathSelectors.any())
                .buld();
        return docket;*/
        return null;
    }

    /**
     * 重写basePackage方法，使能够实现多包访问
     * @param basePackage
     * @return
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage){
        return input -> declaringClass(input).map(handlerPackage(basePackage)::apply).orElse(true);
    }

    /**
     * 重写basePackage方法，使能够实现多包访问
     * @param input
     * @return
     */
    private static Optional<Class<?>> declaringClass(RequestHandler input){
        return Optional.ofNullable(input.declaringClass());
    }

    private static Function<Class<?>,Boolean> handlerPackage(final String basePackage){
        return input -> {
            //循环判断匹配
            for (String strPackage : basePackage.split(SPLITOR)){
                assert input != null;
                boolean isMatch = input.getPackage().getName().equals(strPackage);
                if (isMatch){
                    return true;
                }
            }
            return false;
        };
    }
}
